# 验证access_token和user_id的
import datetime
from flask import Blueprint, request

from static.connect.conn_mysql import Connect

app_access_token_verify = Blueprint('access_token_verify', __name__)


@app_access_token_verify.route('/access_token_verify')
def access_token():
    info = request.args
    access_token = info.get('access_token')
    user_id = info.get('user_id')
    if not all([access_token, user_id]):
        return {
            'status': -1,
            'errorCode': 11,
            'errorMsg': 'access_token为空',
            'resultBody': None
        }
    with Connect() as c:
        select_sql = 'select * from access_token where access_token=%s and user_id=%s'
        result = c.select_one(select_sql, (access_token, user_id))

    if not result:
        return {
            'status': -1,
            'errorCode': 11,
            'errorMsg': 'access_token在数据库中没有匹配的值',
            'resultBody': None
        }
    else:
        token_time = result.get('regedate')
        if token_time + datetime.timedelta(hours=2) <= datetime.datetime.now():
            return {
                'status': -1,
                'errorCode': 12,
                'errorMsg': 'access_token已经过期,请重新登录',
                'resultBody': None
            }
    return {
        'status': 1,
        'errorCode': None,
        'errorMsg': None,
        'resultBody': '验证通过,可以继续操作'
    }
